package com.zero.clinic.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zero.clinic.pojo.*;
import com.zero.clinic.service.JobService;
import com.zero.clinic.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/job")
public class JobController {
    @Resource
    private JobService jobService;
    @Resource
    private UserService userService;

    /**
     * @function 进入职务管理模块
     * @return
     */
    @RequiresPermissions("job:jobPage")
    @RequestMapping("/jobPage")
    public String jobPage(){
        return "jobPage";
    }

    /**
     * @function 职务列表
     * @param pageNum
     * @param pageSize
     * @param keywords
     * @return
     */
    @ResponseBody
    @RequiresPermissions("job:list")
    @RequestMapping("/list")
    public PageInfo<Job> jobList(@RequestParam(defaultValue = "1") int pageNum,
                                   @RequestParam(defaultValue = "10") int pageSize, String keywords){
        PageHelper.startPage(pageNum, pageSize);
        // 1.查出所有的Job信息
        JobExample example = new JobExample();
        // 2 根据搜索关键词查询
        if (StringUtils.isNotBlank(keywords)){
            // 2.1 根据职务名称查询
            JobExample.Criteria criteria= example.createCriteria();
            criteria.andJobNameLike("%"+ keywords +"%");
            // 2.2 根据职务描述查询
            JobExample.Criteria criteria2 = example.or();
            criteria2.andJobDescLike("% + keywords + %");
        }
        List<Job> jobs = jobService.selectByExample(example);
        // 3.以PageInfo对象返回给前端
        PageInfo<Job> pageInfo = new PageInfo<>(jobs);
        return pageInfo;
    }

    /**
     * @function 编辑职务页面
     * @param jobId
     * @param m
     * @return
     */
    @RequestMapping("/edit")
    public String jobEdit(Long jobId, Model m){

        // 1.如果前端页面需要更新职务信息，则会传一个需要更新的jobId过来。这里需要通过jobId查出更新信息后共享给前端页面
        if (jobId != null) {
            Job job = jobService.selectByPrimaryKey(jobId);
            m.addAttribute("job",job);
        }

        return "jobEdit";
    }

    /**
     * @function 新增职务
     * @param job
     * @return
     */
    @ResponseBody
    @RequestMapping("/insert")
    @RequiresPermissions("job:insert")
    public MessageObject jobInsert(Job job){
        //1.插入数据
        System.out.println("需要新增的职务：" + job);
        int row = jobService.insert(job);
        if (row > 0) {
            return MessageObject.createMo(1,"新增数据成功");
        }
        return MessageObject.createMo(0,"新增数据失败，请联系技术人员");
    }

    /**
     * @function 更新职务
     * @param job
     * @return
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("job:update")
    public MessageObject jobUpdate(Job job){
        //1.更新数据
        System.out.println("需要更新的职务" + job);
        int row = jobService.updateByPrimaryKeySelective(job);
        if (row > 0) {
            return MessageObject.createMo(1,"更新数据成功");
        }
        return MessageObject.createMo(0,"更新数据失败，请联系技术人员");
    }

    /**
     * @function 检查职务名称是否重复
     * @param jobName
     * @return
     */
    @ResponseBody
    @RequestMapping("/checkJobName")
    @RequiresPermissions("job:checkJobName")
    public boolean jobCheckJobName(String jobName){
        JobExample example = new JobExample();
        JobExample.Criteria criteria = example.createCriteria();
        criteria.andJobNameEqualTo(jobName);
        List<Job> jobs = jobService.selectByExample(example);
        return jobs.size() > 0 ? false : true;
    }

    /**
     * @function 删除职务
     * @param jobId
     * @return
     */
    @ResponseBody
    @RequestMapping("/delete")
    @RequiresPermissions("job:delete")
    public MessageObject jobDelete(Long jobId){
        //1.删除数据前判断该职务下是否还有员工，如果有则不能删除
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andJobIdEqualTo(jobId);
        List<User> users = userService.selectByExample(example);
        if (users.size() > 0) {
            return MessageObject.createMo(0, "该职务下还有员工，不能删除");
        }

        //2.根据jobId删除数据
        int row = jobService.deleteByPrimaryKey(jobId);
        if (row > 0){
            return MessageObject.createMo(1,"删除数据成功");
        }
        return MessageObject.createMo(0,"删除数据失败，请联系技术人员");
    }

    /**
     * @function 批量删除职务
     * @param jobIds
     * @return
     */
    @ResponseBody
    @RequestMapping("/batchdel")
    @RequiresPermissions("job:batchdel")
    public MessageObject jobBatchDelete(String jobIds){
        //1.删除数据前判断职务下面是否还有员工，如果有则不能删除
        String[] jobIdsArr = jobIds.split(",");
        for (String jobId : jobIdsArr) {
            UserExample example = new UserExample();
            UserExample.Criteria criteria = example.createCriteria();
            criteria.andJobIdEqualTo(Long.parseLong(jobId));
            List<User> users = userService.selectByExample(example);
            if (users.size() > 0) {
                return MessageObject.createMo(0, "被删除的职务下还有员工，不能删除");
            }
        }

        //2.根据jobIdsArr删除数据
        int row = jobService.deleteByIds(jobIdsArr);
        if (row > 0){
            return MessageObject.createMo(1,"删除数据成功");
        }
        return MessageObject.createMo(0,"删除数据失败");
    }

}
